//
//  YKHTTPServiceConfig.m
//  DualMediationSDK
//
//  Created by YeJary on 2025/6/23.
//

#import "YKHTTPServiceConfig.h"
#import "../../Utils/Logger/IMLogUtils.h"

@implementation YKHTTPServiceConfig

static YKHTTPServiceConfig *_sharedConfig = nil;
static dispatch_once_t _onceToken;

+ (instancetype)sharedConfig {
    dispatch_once(&_onceToken, ^{
        _sharedConfig = [[YKHTTPServiceConfig alloc] init];
    });
    return _sharedConfig;
}

- (instancetype)init {
    self = [super init];
    if (self) {
        // 默认配置
        _environment = YKHTTPEnvironmentProduction;
        _productionBaseURL = @"https://sapi.yarkgame.com";
        _testBaseURL = @"https://sapi.yarkgame.com";
        _signKey = @"";
        _enableSignature = YES;
        _gameVersion = @"1.0.0";
        _sdkVersion = @"1.0.0";
        _timezone = @"Asia/Shanghai";
        _timeoutInterval = 30.0;
        _enableLogging = YES;
        _maxRetryCount = 3;
    }
    return self;
}

#pragma mark - Getters

- (NSString *)currentBaseURL {
    switch (self.environment) {
        case YKHTTPEnvironmentProduction:
            return self.productionBaseURL;
        case YKHTTPEnvironmentTest:
            return self.testBaseURL;
        default:
            return self.productionBaseURL;
    }
}

#pragma mark - 配置方法

- (void)configureWithProductionURL:(NSString *)productionURL
                           testURL:(NSString *)testURL
                           signKey:(NSString *)signKey
                       gameVersion:(NSString *)gameVersion
                        sdkVersion:(NSString *)sdkVersion {
    
    if (productionURL) {
        self.productionBaseURL = productionURL;
    }
    
    if (testURL) {
        self.testBaseURL = testURL;
    }
    
    if (signKey) {
        self.signKey = signKey;
    }
    
    if (gameVersion) {
        self.gameVersion = gameVersion;
    }
    
    if (sdkVersion) {
        self.sdkVersion = sdkVersion;
    }
    
    if (self.enableLogging) {
        [IMLogUtils info:IMLogTagNetwork format:@"[YKHTTPService] 配置完成 - 生产环境: %@, 测试环境: %@, 游戏版本: %@, SDK版本: %@", 
         productionURL, testURL, gameVersion, sdkVersion];
    }
}

- (void)switchToProduction {
    self.environment = YKHTTPEnvironmentProduction;
    
    if (self.enableLogging) {
        [IMLogUtils info:IMLogTagNetwork format:@"[YKHTTPService] 切换到生产环境: %@", self.currentBaseURL];
    }
}

- (void)switchToTest {
    self.environment = YKHTTPEnvironmentTest;
    
    if (self.enableLogging) {
        [IMLogUtils info:IMLogTagNetwork format:@"[YKHTTPService] 切换到测试环境: %@", self.currentBaseURL];
    }
}

- (NSString *)currentEnvironmentDescription {
    switch (self.environment) {
        case YKHTTPEnvironmentProduction:
            return @"生产环境";
        case YKHTTPEnvironmentTest:
            return @"测试环境";
        default:
            return @"未知环境";
    }
}

@end
